program allperm
    implicit none
    integer,parameter::n=3
    integer::i
    integer::a(n)
    a = [(i,i=1,n)]
    call perm(1)
contains
    recursive subroutine perm(i)
        integer::i,j
        if(i==n)then
            write(*,"(*(3I3))")a
        else
            do j=i, n
                a([i,j])=a([j,i])
                call perm(i + 1)
                a([i,j])=a([j,i])
            end do
        end if
    end subroutine
end program